package org.voidness.oje2d.tests;

import org.lwjgl.input.Keyboard;
import org.voidness.oje2d.Engine;
import org.voidness.oje2d.GLAnimatedImage;
import org.voidness.oje2d.GLColor;
import org.voidness.oje2d.GLFlyingText;
import org.voidness.oje2d.GLFont;
import org.voidness.oje2d.GLImage;
import org.voidness.oje2d.particlesystems.GLParticleSystemExplode;
import org.voidness.oje2d.particlesystems.GLParticleSystemRing;
import org.voidness.oje2d.particlesystems.GLParticleSystemSnow;
import org.voidness.oje2d.particlesystems.GLParticleSystemSpiralRing;
import org.voidness.oje2d.gui.Button;
import org.voidness.oje2d.gui.GuiEngine;
import org.voidness.oje2d.gui.Listener;
import org.voidness.oje2d.gui.TextBox;
import org.voidness.oje2d.gui.Window;
import org.voidness.oje2d.gui.docks.DockedWindow;
import org.voidness.oje2d.interfaces.EngineInterface;

public class EngineTest implements EngineInterface {
    private final int WIDTH = 800;

    private final int HEIGHT = 600;

    private final int DEPTH = 16;

    private final boolean FULLSCREEN = false;

    private Engine engine = null;

    private GLImage dukebox = null;

    private GLImage crate = null;

    private GLFont font = null;

    private GLAnimatedImage character = null;

    private GLAnimatedImage character2 = null;

    private GLAnimatedImage fire = null;

    private GLParticleSystemRing particleRing = null;

    private GLParticleSystemExplode particleExplosion = null;

    private GLParticleSystemSpiralRing particleSpiral = null;

    private GLParticleSystemSnow particleSnow = null;

    private TextBox textBox = null;

    private GLFlyingText flyingText = null;

    public EngineTest() {
        engine = Engine.getInstance();
        engine.init(WIDTH, HEIGHT, DEPTH, FULLSCREEN, this);
        engine.start();
    }

    public void checkKeys() {
        if (engine.isKeyPressed(Keyboard.KEY_ESCAPE)) {
            engine.destroy();
            System.exit(1);
        }

        if (engine.isKeyPressed(Keyboard.KEY_SPACE)) {
            particleExplosion.start();
            particleSpiral.start();
            particleSnow.start();
        }

        if (engine.isKeyPressed(Keyboard.KEY_ADD)) {
            flyingText =
                    new GLFlyingText("Critical!", font, 1000, true, 50, 0.8f,
                            1.5f);
        }

        if (engine.isKeyPressed(Keyboard.KEY_SUBTRACT)) {
            flyingText =
                    new GLFlyingText("-20", font, 1000, true, 50, 0.8f, 1.5f);
        }

        if (engine.getMouseLeftButtonDown() && particleExplosion.isDead())
            particleExplosion.start();
    }

    public void drawText() {
        font.print("FPS: " + engine.getFPS(), 10, 10, 1.0f, GLColor.GREEN);
        font.print("Mouse: " + engine.getMouseX() + "," + engine.getMouseY(),
                10, 30, 1.0f, GLColor.YELLOW);
        font.print("Left button: " + engine.getMouseLeftButtonDown(), 10, 50,
                0.6f, GLColor.RED);
        font.print("Right button: " + engine.getMouseRightButtonDown(), 10, 70,
                0.6f, GLColor.BLUE);
        font.print("Ouch ... it burns!", 90, 350, 0.75f, GLColor.YELLOW);
        font.print("Mouse moving: " + engine.mouseMoved(), 10, 370, 0.75f,
                GLColor.YELLOW);

        font.print("Image under mouse ID: " + engine.getImageUnderMouseId(),
                10, 420, 0.75f, GLColor.WHITE);

        font.print("Alive particles: " + particleSnow.getLiveParticleCount(),
                10, 510, 0.8f, GLColor.YELLOW);
        font.print("Snow alive?: " + !particleSnow.isDead(), 10, 530, 0.8f,
                GLColor.YELLOW);
    }

    public void initChild() {
        engine.setBackgroundColor(GLColor.GRAY);

        crate = new GLImage("resources/images/crate.jpg");
        dukebox = new GLImage("resources/images/character.png");
        dukebox.setId(0);
        font = new GLFont("resources/fonts/tahoma.png", 1.0f);

        flyingText =
                new GLFlyingText("Critical!", font, 1000, true, 100, 0.8f, 1.5f);

        // Animated image
        character =
                new GLAnimatedImage("resources/images/character.png", 64, 64,
                        64, 64, 4);
        character.setUpdateInterval(200);
        character.setAnimationLoop(true);

        // Animated image
        character2 =
                new GLAnimatedImage("resources/images/character2.png", 32, 64,
                        32, 64, 3);
        character2.setUpdateInterval(300);
        character2.setAnimationLoop(true);

        // Animated image
        fire =
                new GLAnimatedImage("resources/images/fire.png", 32, 64, 64,
                        128, 8);
        fire.setUpdateInterval(100);
        fire.setAnimationLoop(true);

        // Particle ring
        particleRing = new GLParticleSystemRing();
        particleRing.initParticles("resources/images/particle.png", 100);
        particleRing.setRadius(90, 120);
        particleRing.setSpeed(0.5f, 5);
        particleRing.setTimeToLive(5000, 10000);
        particleRing.setLoop(true);
        particleRing.start();

        // Explosion
        particleExplosion = new GLParticleSystemExplode();
        particleExplosion.initParticles("resources/images/particle.png", 100);
        particleExplosion.setRadius(0, 50);
        particleExplosion.setSpeed(0.1f, 3);
        particleExplosion.setTimeToLive(4000, 5000);

        // Spiral ring
        particleSpiral = new GLParticleSystemSpiralRing();
        particleSpiral.initParticles("resources/images/particle.png", 200);
        particleSpiral.setRadius(0, 100);
        particleSpiral.setRadiusSpeed(0.5f, 3);
        particleSpiral.setRotationSpeed(1, 20);
        particleSpiral.setTimeToLive(4000, 5000);

        // Snow (or rain) system
        particleSnow = new GLParticleSystemSnow();
        particleSnow.initParticles("resources/images/particle.png", 100);
        particleSnow.setArea(0, 0, 800, 600);
        particleSnow.setSpeed(2, 5);
        particleSnow.setTimeToLive(4000, 5000);
        particleSnow.setAlphaStep(0.005f);
        particleSnow.setLoop(true);

        // ***************************** GUI Testing

        // Let's create a window
        Window window = new Window(200, 200, 100, 150);
        Window window2 = new Window(300, 300, 150, 250);
        Window window3 = new Window(400, 400, 100, 200);

        GLImage[] corners = new GLImage[4];
        String[] faces = new String[4];
        String back = new String();
        corners[0] =
                new GLImage("resources/images/gui/topLeft.png", 10, 10, false);
        corners[1] =
                new GLImage("resources/images/gui/topRight.png", 10, 10, false);
        corners[2] =
                new GLImage("resources/images/gui/bottomLeft.png", 10, 10,
                        false);
        corners[3] =
                new GLImage("resources/images/gui/bottomRight.png", 10, 10,
                        false);
        faces[0] = new String("resources/images/gui/topCenter.png");
        faces[1] = new String("resources/images/gui/left.png");
        faces[2] = new String("resources/images/gui/right.png");
        faces[3] = new String("resources/images/gui/bottomCenter.png");
        back = new String("resources/images/gui/back.png");

        window.setImages(corners, faces, back);
        window2.setImages(corners, faces, back);
        window3.setImages(corners, faces, back);

        // Create a button and add to the window
        Button button = new Button(10, 10, 32, 10);
        GLImage[] buttonImages = new GLImage[3];
        buttonImages[Button.NORMAL] =
                new GLImage("resources/images/gui/buttonNormal.png", 32, 10,
                        false);
        buttonImages[Button.OVER] =
                new GLImage("resources/images/gui/buttonOver.png", 32, 10,
                        false);
        buttonImages[Button.PRESSED] =
                new GLImage("resources/images/gui/buttonPressed.png", 32, 10,
                        false);
        button.setImages(buttonImages);
        button.addListener(new Listener() {
            public void mousePressed(int mButton) {
            }

            public void mouseReleased(int mButton) {
                System.out.println("Button action");
            }

            public void keyPressed(int mKey) {
                if (mKey == Keyboard.KEY_RETURN)
                    System.out.println("Button action");
            }

            public void keyReleased(int mKey) {
            }
        });
        button.setToolTip("Um botao", font, 2000);
        window.add(button);

        // Create a textbox and add to another window
        textBox = new TextBox(10, 10, 80, 15, font, 10);
        GLImage[] textBoxImages = new GLImage[2];
        textBoxImages[TextBox.NORMAL] =
                new GLImage("resources/images/gui/textBoxNormal.png", 80, 15,
                        false);
        textBoxImages[TextBox.FOCUS] =
                new GLImage("resources/images/gui/textBoxFocus.png", 80, 15,
                        false);
        textBox.setImages(textBoxImages);
        textBox.addListener(new Listener() {
            public void mousePressed(int mButton) {
            }

            public void mouseReleased(int mButton) {
            }

            public void keyPressed(int mKey) {
                if (mKey == Keyboard.KEY_RETURN)
                    System.out.println(textBox.getText());
            }

            public void keyReleased(int mKey) {
                System.out.println("Key released: " + mKey);
            }
        });
        window2.add(textBox);

        window3.setToolTip("Janela 3", font, 500);

        // **************************** Docked windows tests

        DockedWindow dockedWindow =
                new DockedWindow(0, 20, 100, 150, DockedWindow.LEFT);
        GLImage dockedWindowImage =
                new GLImage("resources/images/gui/coolWindow/back.png", 100,
                        150, false);
        GLImage[] dockedWindowEdgeImages = new GLImage[2];
        dockedWindowEdgeImages[0] =
                new GLImage("resources/images/gui/coolWindow/topRight.png", 10,
                        10, false);
        dockedWindowEdgeImages[1] =
                new GLImage("resources/images/gui/coolWindow/bottomRight.png",
                        10, 10, false);
        String dockedWindowEdgeBack =
                new String("resources/images/gui/coolWindow/right.png");
        dockedWindow.setBodyImage(dockedWindowImage);
        dockedWindow
                .setEdgeImages(dockedWindowEdgeImages, dockedWindowEdgeBack);

        DockedWindow dockedWindow2 =
                new DockedWindow(
                        Engine.getInstance().getDisplayBounds().getX(), 20,
                        100, 150, DockedWindow.RIGHT);
        GLImage dockedWindowImage2 =
                new GLImage("resources/images/gui/coolWindow/back.png", 100,
                        150, false);
        GLImage[] dockedWindowEdgeImages2 = new GLImage[2];
        dockedWindowEdgeImages2[0] =
                new GLImage("resources/images/gui/coolWindow/topLeft.png", 10,
                        10, false);
        dockedWindowEdgeImages2[1] =
                new GLImage("resources/images/gui/coolWindow/bottomLeft.png",
                        10, 10, false);
        String dockedWindowEdgeBack2 =
                new String("resources/images/gui/coolWindow/left.png");
        dockedWindow2.setBodyImage(dockedWindowImage2);
        dockedWindow2.setEdgeImages(dockedWindowEdgeImages2,
                dockedWindowEdgeBack2);

        DockedWindow dockedWindow3 =
                new DockedWindow(400, 0, 150, 100, DockedWindow.TOP);
        GLImage dockedWindowImage3 =
                new GLImage("resources/images/gui/coolWindow/back.png", 150,
                        100, false);
        GLImage[] dockedWindowEdgeImages3 = new GLImage[2];
        dockedWindowEdgeImages3[0] =
                new GLImage("resources/images/gui/coolWindow/bottomLeft.png",
                        10, 10, false);
        dockedWindowEdgeImages3[1] =
                new GLImage("resources/images/gui/coolWindow/bottomRight.png",
                        10, 10, false);
        String dockedWindowEdgeBack3 =
                new String("resources/images/gui/coolWindow/bottomCenter.png");
        dockedWindow3.setBodyImage(dockedWindowImage3);
        dockedWindow3.setEdgeImages(dockedWindowEdgeImages3,
                dockedWindowEdgeBack3);

        DockedWindow dockedWindow4 =
                new DockedWindow(400, Engine.getInstance().getDisplayBounds()
                        .getY(), 150, 100, DockedWindow.BOTTOM);
        GLImage dockedWindowImage4 =
                new GLImage("resources/images/gui/coolWindow/back.png", 150,
                        100, false);
        GLImage[] dockedWindowEdgeImages4 = new GLImage[2];
        dockedWindowEdgeImages4[0] =
                new GLImage("resources/images/gui/coolWindow/topLeft.png", 10,
                        10, false);
        dockedWindowEdgeImages4[1] =
                new GLImage("resources/images/gui/coolWindow/topRight.png", 10,
                        10, false);
        String dockedWindowEdgeBack4 =
                new String("resources/images/gui/coolWindow/topCenter.png");
        dockedWindow4.setBodyImage(dockedWindowImage4);
        dockedWindow4.setEdgeImages(dockedWindowEdgeImages4,
                dockedWindowEdgeBack4);

        // **************************** Docked windows tests end

        // and add it to the GUI engine
        GuiEngine.getInstance().addWindow(window);
        GuiEngine.getInstance().addWindow(window2);
        GuiEngine.getInstance().addWindow(window3);
        GuiEngine.getInstance().addWindow(dockedWindow);
        GuiEngine.getInstance().addWindow(dockedWindow2);
        GuiEngine.getInstance().addWindow(dockedWindow3);
        GuiEngine.getInstance().addWindow(dockedWindow4);
    }

    public void render() {
        logic();
        checkKeys();

        drawTiles();
        drawText();
    }

    public void drawTiles() {
        /*
         * for (int row = 0; row < WIDTH / crate.getWidth(); row++) for (int
         * column = 0; column <= HEIGHT / crate.getHeight(); column++)
         * crate.draw(row crate.getWidth(), column crate.getHeight());
         */

        crate.drawRotated(400, 100, 45.0f);

        character.draw(100, 500);
        flyingText.draw(215, 490, new GLColor(1.0f, 1.0f, 0.0f, 1.0f));
        character2.draw(200, 500);
        fire.draw(100, 470);

        dukebox.draw(300, 200);

        particleRing.draw(engine.getMouseX(), engine.getMouseY());
        particleExplosion.draw(engine.getMouseX(), engine.getMouseY());
        particleSpiral.draw(700, 300);
        particleSnow.draw();

        /*
         * for (int row=0; row<WIDTH/dukebox.getWidth(); row++) for (int
         * column=0; column<=HEIGHT/dukebox.getHeight(); column++)
         * dukebox.draw(rowdukebox.getWidth(),
         * HEIGHT-columndukebox.getHeight());
         */
    }

    public void logic() {

    }

    public static void main(String[] args) {
        new EngineTest();
    }

    public void mousePressed(int mButton) {
    }

    public void mouseReleased(int mButton) {

    }

    public void onEngineExit() {
    }
}